<template>
  <div class="update-birthday">
    <!-- currentDate 双向绑定日期选择器
     -->
    <van-datetime-picker

        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="$emit('close')"
        @confirm="onConfirm"
      />
  </div>
</template>

<script>
import { updataUserProfile } from '@/api/user'
import dayjs from 'dayjs'
export default {
  name: 'UpdateBirthday',
  props: {
    value: {
      type: String,
      require: true
    }
  },
  data () {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },

  mounted () {

  },

  methods: {
    async onConfirm () {
      try {
        this.$toast.loading({
          message: '保存中...',
          forbidClick: true, // 禁用背景点击
          duration: 0 // 如果为0，则持续展示

        })
        const currentDate = dayjs(this.currentDate).format('YYYY-MM-DD')

        await updataUserProfile({
          birthday: currentDate
        })
        // 更新视图
        this.$emit('input', currentDate)
        this.$emit('close')
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
